package com.yuyou.tas.lesson.service.impl;

import com.yuyou.tas.lesson.mapper.BaseSectionSubjectRelMapper;
import com.yuyou.tas.lesson.mapper.CommDataConfigMapper;
import com.yuyou.tas.lesson.service.ICommDataConfigService;
import com.yuyou.tas.service.api.admin.dto.AuthUserSubjectDTO;
import com.yuyou.tas.service.api.admin.entity.CommDataConfig;
import com.yuyou.tas.service.api.admin.service.AuthUserAPI;
import com.yuyou.tas.service.api.lesson.entity.BaseSectionSubjectRel;
import com.yuyou.tas.util.tool.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommDataConfigServiceImpl implements ICommDataConfigService {

	@Autowired
	private CommDataConfigMapper commDataConfigMapper;

	@Autowired
	private BaseSectionSubjectRelMapper baseSectionSubjectRelMapper;

	@Autowired
	private AuthUserAPI authUserAPI;

	@Override
	public List<CommDataConfig> findUseByCategory(Integer category) {
		return commDataConfigMapper.findUseByCategory(category);
	}

	@Override
	public List<CommDataConfig> findSubject(Map<String, Object> condition) {
		return commDataConfigMapper.findSubject(condition);
	}



	@Override
	public List<CommDataConfig> findMaterial(Map<String, Object> condition) {
		return commDataConfigMapper.findMaterial(condition);
	}

	@Override
	public CommDataConfig selectByCode(String code) {
		return commDataConfigMapper.selectByCode(code);
	}

	@Override
	public List<CommDataConfig> findUserSubject(Long userId , Long sectionId) {
		List<CommDataConfig> subjects = commDataConfigMapper.findSubjectFromClass(userId);
		if(sectionId != null){
			Map<String, Object> conditionsMap = new HashMap<>();
			conditionsMap.put("isStan", 1);
			conditionsMap.put("sectionId", sectionId);
			List<BaseSectionSubjectRel> rels = baseSectionSubjectRelMapper.findSSRs(conditionsMap);
			Map<Long , BaseSectionSubjectRel> map = BeanUtils.toMap(rels , "dataSubject");

			return subjects.stream().filter(s -> map.containsKey(s.getId())).collect(Collectors.toList());
		}
		return subjects;
	}

	@Override
	public List<CommDataConfig> findTeachingSubject(Long userId, Long sectionId) {
		List<AuthUserSubjectDTO> subjectDTOS = authUserAPI.findSubjectByAuthUserId(userId);
		List<CommDataConfig> subjects = new ArrayList<>();

		subjectDTOS.forEach(a -> {
			CommDataConfig c = new CommDataConfig();
			c.setId(a.getDataSubject());
			c.setName(a.getSubjectName());
			subjects.add(c);
		});

		if(sectionId != null){
			Map<String, Object> conditionsMap = new HashMap<>();
			conditionsMap.put("isStan", 1);
			conditionsMap.put("sectionId", sectionId);
			List<BaseSectionSubjectRel> rels = baseSectionSubjectRelMapper.findSSRs(conditionsMap);
			Map<Long , BaseSectionSubjectRel> map = BeanUtils.toMap(rels , "dataSubject");

			return subjects.stream().filter(s -> map.containsKey(s.getId())).collect(Collectors.toList());
		}

		return subjects;
	}


}